﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SolutionBLL;
using SolutionModel;
using SFast;
using SFast.Web;

public partial class admin_system_MenuDetail : System.Web.UI.Page
{


    T_MenuBLL bll = new T_MenuBLL();
    protected String btn_text = "确  定";

    protected void Page_Load(object sender, EventArgs e)
    {
        String act = Request.QueryString["act"];  //url中的act
        String Action = Request.Form["action"];   //表单中的action

    }

    #region 添加时加载
    public void add_Load()
    {
        this.form1.Action = "menudetail.aspx?fun=add_onSave";
        btn_text = "提  交";
    }
    #endregion

    #region 编辑时加载
    public void edit_Load()
    {
        btn_text = "保  存";
        this.form1.Action = "menudetail.aspx?fun=edit_onSave";
    }
    #endregion

    #region  添加时保存

    public String add_onSave()
    {
        MessageInfoByFormModel info = new MessageInfoByFormModel();
        try
        {
            int rowCount = bll.InsertMenus(Request._GetForm<T_Menu>());
            if (rowCount > 0)
            {
                info.info = "菜单添加成功！";
                info.status = "y";
            }
            else
            {
                info.info = "菜单添加失败！";
                info.status = "n";
            }
            return info._ToJsonStr();
        }
        catch (Exception emsg)
        {
            info.info = emsg.Message;
            info.status = "n";
            return info._ToJsonStr();
        }
    }
    #endregion

    #region  编辑时保存

    public String edit_onSave()
    {
        MessageInfoByFormModel info = new MessageInfoByFormModel();
        try
        {
            T_Menu menu = Request._GetForm<T_Menu>();
            int re = bll.VerifyParent(Convert.ToInt32(menu.C_Parent_ID), menu.C_ID);
            if (re > 0)
            {
                info.info = "不可把“菜单本身”或“子菜单”当做本菜单的“父菜单”，请重新选择父菜单项！";
                info.status = "y";
                return info._ToJsonStr();
            }
            int rowCount = bll.UpdateMenu(menu);
            if (rowCount > 0)
            {
                info.info = "菜单保存成功！";
                info.status = "y";
            }
            else
            {
                info.info = "菜单保存失败！";
                info.status = "n";
            }
            return info._ToJsonStr();
        }
        catch (Exception emsg)
        {
            info.info = emsg.Message;
            info.status = "n";
            return info._ToJsonStr();
        }
    }
    #endregion


    //删除菜单
    public String delMenu()
    {
        MessageInfoByFormModel info = new MessageInfoByFormModel();
        int id = 0;
        if (Request.QueryString["id"]._HasVal())
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
        }

        if (bll.GetTreeByPid(id).Count() > 0)
        {
            info.info = "当前分类有子分类，不可以删除，请先删除子分类再删除此分类。";
            info.status = "n";
            return info._ToJsonStr();
        }

        bll.delMenuID(id);
        info.info = "删除成功！";
        info.status = "y";
        return info._ToJsonStr();
    }

}